package net.caixiaomi.info.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import net.caixiaomi.info.R;
import net.caixiaomi.info.base.quickadapter.BaseMultiItemQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.OpenPrizeEntity;
import net.caixiaomi.info.util.Constants;

import java.util.List;

/**
 * Created by mac on 2018/9/6.
 */

public class OpenPrizeAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public OpenPrizeAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(Constants.ITEM_MATCH_LIVE0, R.layout.open_prize_item_layout);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        OpenPrizeEntity entity= (OpenPrizeEntity) item;
        helper.setText(R.id.prize_lottery_name,entity.getLotteryName());
        if(entity.getLotteryId().equals("1")){
            helper.setText(R.id.prize_lottery_period,entity.getChangci());
        }else{
            helper.setText(R.id.prize_lottery_period,entity.getPeriod());
        }
        helper.setText(R.id.prize_lottery_date,entity.getDate());
        ImageView imageView = helper.getView(R.id.prize_logo);
        Glide.with(imageView.getContext()).load(entity.getLotteryIcon()).into(imageView);
        LinearLayout rroup=helper.getView(R.id.open_prize_content_group);
        rroup.removeAllViews();
        View contentView=null;
        LinearLayout ballgroup=null;

        ImageView voal_left=null;
        ImageView voal_right=null;
        LinearLayout voal_group=null;
        TextView open_sports_name0;
        TextView open_sports_name1;
        TextView open_sports_name2;

        switch (entity.getLotteryId()){
            case "1":   //竞彩足球
                contentView=View.inflate(mContext,R.layout.open_sports_ball_layout,null);
                contentView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                rroup.addView(contentView);

                initSportsView(entity,contentView);
                break;
            case "2":   //大乐透
                contentView=View.inflate(mContext,R.layout.open_normal_ball_layout,null);
                contentView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                rroup.addView(contentView);
                ballgroup=contentView.findViewById(R.id.ball_group);
                initBallView(entity,ballgroup);
                break;
            case "3":   //竞彩篮球
                contentView=View.inflate(mContext,R.layout.open_sports_ball_layout,null);
                contentView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                rroup.addView(contentView);
                initSportsView(entity,contentView);
                break;
            case "4":   //快3
                contentView=View.inflate(mContext,R.layout.open_normal_ball_layout,null);
                contentView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                rroup.addView(contentView);
                ballgroup=contentView.findViewById(R.id.ball_group);
                initBallView(entity,ballgroup);
                break;
            case "5":   //双色球
                contentView=View.inflate(mContext,R.layout.open_normal_ball_layout,null);
                contentView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                rroup.addView(contentView);
                ballgroup=contentView.findViewById(R.id.ball_group);
                initBallView(entity,ballgroup);
                break;
            case "6":   //北单
                contentView=View.inflate(mContext,R.layout.open_sports_ball_layout,null);
                contentView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                rroup.addView(contentView);
                initSportsView(entity,contentView);
                break;
            case "7":   //11选5
                contentView=View.inflate(mContext,R.layout.open_normal_ball_layout,null);
                contentView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                rroup.addView(contentView);
                ballgroup=contentView.findViewById(R.id.ball_group);
                initBallView(entity,ballgroup);
                break;
            case "8":   //更多彩种

                break;
        }
    }

    private void initSportsView(OpenPrizeEntity entity,View contentView){

        ImageView voal_left=contentView.findViewById(R.id.voal_left);
        ImageView voal_right=contentView.findViewById(R.id.voal_right);
        RelativeLayout voal_group=contentView.findViewById(R.id.voal_group);
        TextView open_sports_name0=contentView.findViewById(R.id.open_sports_name0);
        TextView open_sports_name1=contentView.findViewById(R.id.open_sports_name1);
        TextView open_sports_name2=contentView.findViewById(R.id.open_sports_name2);

        open_sports_name0.setText(entity.getHomeTeam());
        open_sports_name1.setText(entity.getScore());
        open_sports_name2.setText(entity.getVisitingTeam());
        if(entity.getLotteryId().equals("3")){
            voal_left.setBackgroundResource(R.drawable.oval_orange_left);
            voal_right.setBackgroundResource(R.drawable.oval_orange_right);
            voal_group.setBackgroundResource(R.color.orange_oval);
        }else{
            voal_left.setBackgroundResource(R.drawable.oval_green_left);
            voal_right.setBackgroundResource(R.drawable.oval_green_right);
            voal_group.setBackgroundResource(R.color.green_oval);
        }

    }

    private void initBallView(OpenPrizeEntity entity,LinearLayout group){
        switch (entity.getLotteryId()){
            case "2":
                for(int i=0;i<5;i++){
                    LinearLayout rl= (LinearLayout) group.getChildAt(i);
                    TextView tv= (TextView) rl.getChildAt(0);
                    tv.setBackgroundResource(R.drawable.red_ball);
                    String content=entity.getRedBall().get(i);
                    tv.setText(content);
                }
                for(int i=5;i<7;i++){
                    LinearLayout rl= (LinearLayout) group.getChildAt(i);
                    TextView tv= (TextView) rl.getChildAt(0);
                    tv.setBackgroundResource(R.drawable.blue_ball);
                    String content=entity.getBlueBall().get(i-5);
                    tv.setText(content);
                }
                break;
//            case "4":
//                group.getChildAt(3).setVisibility(View.INVISIBLE);
//                group.getChildAt(4).setVisibility(View.INVISIBLE);
//                group.getChildAt(5).setVisibility(View.INVISIBLE);
//                group.getChildAt(6).setVisibility(View.INVISIBLE);
//                for(int i=0;i<3;i++){
//                    LinearLayout rl1= (LinearLayout) group.getChildAt(i);
//                    TextView tv1= (TextView) rl1.getChildAt(0);
//                    tv1.setBackgroundResource(R.drawable.red_ball);
//                    String content1=entity.getRedBall().get(i);
//                    tv1.setText(content1);
//                }
//                break;
//            case "5":
//                for(int i=0;i<6;i++){
//                    LinearLayout rl= (LinearLayout) group.getChildAt(i);
//                    TextView tv= (TextView) rl.getChildAt(0);
//                    tv.setBackgroundResource(R.drawable.red_ball);
//                    String content=entity.getRedBall().get(i);
//                    tv.setText(content);
//                }
//                LinearLayout rl= (LinearLayout) group.getChildAt(6);
//                TextView tv= (TextView) rl.getChildAt(0);
//                tv.setBackgroundResource(R.drawable.blue_ball);
//                String content=entity.getBlueBall().get(0);
//                tv.setText(content);
//                break;
//            case "7":
//                group.getChildAt(5).setVisibility(View.INVISIBLE);
//                group.getChildAt(6).setVisibility(View.INVISIBLE);
//                for(int i=0;i<5;i++){
//                    LinearLayout rl1= (LinearLayout) group.getChildAt(i);
//                    TextView tv1= (TextView) rl1.getChildAt(0);
//                    tv1.setBackgroundResource(R.drawable.red_ball);
//                    String content1=entity.getRedBall().get(i);
//                    tv1.setText(content1);
//                }
//                break;
        }
    }

}
